﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nova_BLL
{
    public class MaterialType
    {
        int i = 0;
        DataSet ds = null;
        Nova_DAL.MaterialType Nova_DAL_MaterialType = new Nova_DAL.MaterialType();

        public DataSet ListMaterialType()
        {
            try
            {

                ds = Nova_DAL_MaterialType.ListMaterialType();
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }


        /// <summary>
        /// 获取指定类别ID的类别信息
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <returns></returns>
        public DataSet ListMaterialType(string sTypeID)
        {
            try
            {
                ds = Nova_DAL_MaterialType.ListMaterialType(sTypeID); ;
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 根据TypeID 删除物料类别
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>1：删除成功  -1：异常  0：没有删除，以及原因</returns>
        public int DelMaterialType(string sTypeID, out string strMessage)
        {
            int res = 0;
            strMessage = "";
            try
            {
                //1、根据物料类别主键TypeID 判断类别的是否为父类别
                res = Nova_DAL_MaterialType.JudgeFatherType(sTypeID, out strMessage);
                if (res == -1)
                {
                    return res;
                }

                //2、根据物料类别，判断能否删除
                //父类别的是否关联着子类 是否能够删除
                if (res == 1)
                {
                    res = Nova_DAL_MaterialType.JudageDelFatherType(sTypeID, out strMessage);
                    if (res != 1)
                    {

                        return res;
                    }

                }

                //子类是否关联着物料 是否能够删除
                if (res == 0) //
                {
                    res = Nova_DAL_MaterialType.JudageDelFatherType(sTypeID, out strMessage);
                    if (res != 1)
                    {

                        return res;
                    }
                }


                //3、删除
                res = Nova_DAL_MaterialType.DelMaterialType(sTypeID, out strMessage);

            }
            catch
            {
                res = -1;
                strMessage = "根据TypeID 删除物料类别方法异常跳出";
            }

            return res;
        }



        /// <summary>
        /// 获取新的父类别ID 例如：B01
        /// </summary>
        /// <returns>""获取失败，非""获取成功</returns>
        public string GetNewParentTypeID(out string strMessage)
        {
            return Nova_DAL_MaterialType.GetNewParentTypeID(out strMessage);
        }

        /// <summary>
        /// 获取当前父类下的子类ID编码 例如：B01001
        /// </summary>
        /// <param name="ParentTypeID">上一级类ID编码</param>
        /// <param name="strMessage"></param>
        /// <returns>""获取失败，非""获取成功</returns>
        public string GetNewSubTypeID(string ParentTypeID, out string strMessage)
        {
            return Nova_DAL_MaterialType.GetNewSubTypeID(ParentTypeID, out strMessage);
        }

        /// <summary>
        /// 根据子类ID 获取子类下 新的物料编码 例如：B010010001
        /// </summary>
        /// <param name="SubTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public string GetNewMaterialID(string SubTypeID, out string strMessage)
        {
            return Nova_DAL_MaterialType.GetNewMaterialID(SubTypeID, out strMessage);
        }

        /// <summary>
        /// 插入物料类别表
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="sTypeName"></param>
        /// <param name="ParentTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败 1：成功</returns>
        public int InsertMaterialType(string sTypeID, string sTypeName, string sParentTypeID, out string strMessage)
        {
            return Nova_DAL_MaterialType.InsertMaterialType(sTypeID, sTypeName, sParentTypeID, out strMessage);
        }


        /// <summary>
        /// 更新物料类别信息 根据主键TypeID
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="sTypeName"></param>
        /// <param name="sParentTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败 1：成功</returns>
        public int UpdateMaterialType(string sTypeID, string sTypeName, string sParentTypeID, out string strMessage)
        {
            return Nova_DAL_MaterialType.UpdateMaterialType(sTypeID, sTypeName, sParentTypeID, out strMessage);
        }


        /// <summary>
        /// 获取父类别的数据集
        /// </summary>
        /// <returns></returns>
        public DataSet ListParentType()
        {
            return Nova_DAL_MaterialType.ListParentType();
        }

        /// <summary>
        /// 获取指定父类 下的子类别信息集合
        /// </summary>
        /// <param name="TypeID"></param>
        /// <returns></returns>
        public DataSet ListSubType(string ParentTypeID)
        {
            return Nova_DAL_MaterialType.ListSubType(ParentTypeID);
        }

    }
}


 